/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.com.pps.web.common.component.table;

import java.util.ArrayList;
import java.util.Iterator;
import javax.faces.context.FacesContext;
import vn.com.pps.web.servicelocator.IServiceLocator;

public abstract class LazyCacheModel extends AbstractModel {

    private int m_from;
    private int m_to;
    protected ArrayList m_cachedData;
    private String m_sortColumn = "";
    private boolean m_sortAscending;
    private boolean m_newSortColumn;
    private boolean m_newSortAscending;
    private FacesContext facesContext = FacesContext.getCurrentInstance();

    protected void init() {
    }
    private IServiceLocator serviceLocator;

    public IServiceLocator getServiceLocator() {
        return serviceLocator;
    }

    public void setServiceLocator(IServiceLocator serviceLocator) {
        this.serviceLocator = serviceLocator;
        this.init();
    }

    public synchronized void addItem(AbstractTableItem pm_item) {
        this.m_cachedData.add(pm_item);
    }

    public synchronized void deleteItems() {
        this.m_cachedData.clear();
    }

    public synchronized void setItemIndexes(int pm_from, int pm_to) {
        this.m_from = pm_from;
        this.m_to = pm_to;
    }
    // protected abstract void loadItems(int paramInt1, int paramInt2);

    public void setAllSelectedStatus(boolean pm_status) {
        Iterator iter = this.m_cachedData.iterator();
        while (iter.hasNext()) {
            AbstractTableItem item = (AbstractTableItem) iter.next();
            item.setSelected(pm_status);
        }
    }

    public String getSort() {
        return this.m_sortColumn;
    }

    public void setSort(String pm_column) {
        if ((this.m_sortColumn == null) || (!this.m_sortColumn.equals(pm_column))) {
            this.m_newSortColumn = true;
        } else {
            this.m_newSortColumn = false;
        }
        this.m_sortColumn = pm_column;
    }

    public boolean isSortAscending() {
        return this.m_sortAscending;
    }

    public void setSortAscending(boolean pm_sortAscending) {
        if (this.m_sortAscending != pm_sortAscending) {
            this.m_newSortAscending = true;
        } else {
            this.m_newSortAscending = false;
        }
        if ((this.m_newSortAscending) && (!this.m_newSortColumn)) {
            this.m_sortAscending = pm_sortAscending;
        }
        if ((!this.m_newSortColumn) && (!this.m_newSortAscending)) {
            return;
        }
        sort(getSort(), this.m_sortAscending);
        //displayTargetPage();
    }

    public void setTargetPage(int pm_value) {
        this.m_targetPage = pm_value;
    }

    public int getTargetPage() {
        return this.m_targetPage;
    }

    @Override
    public synchronized boolean getHasNextPage() {

        return this.m_selectedPage != this.m_pageCount;
    }

    @Override
    public synchronized boolean getHasPreviousPage() {
        return this.m_selectedPage > 1;
    }

    @Override
    public synchronized void firstPage() {
        this.m_selectedPage = (this.m_targetPage = 0);
        nextPage();
    }

    @Override
    public synchronized void lastPage() {
        this.m_selectedPage = (this.m_targetPage = this.m_pageCount);
        loadPage();
    }

    @Override
    public synchronized void nextPage() {
        this.m_selectedPage = (this.m_targetPage = Math.min(this.m_pageCount, this.m_selectedPage + 1));
        loadPage();
    }

    @Override
    public synchronized void previousPage() {
        this.m_selectedPage = (this.m_targetPage = Math.max(0, this.m_selectedPage - 1));
        if ((this.m_selectedPage == 0) && (this.m_pageCount != 0)) {
            this.m_selectedPage = (this.m_targetPage = 1);
        }
        loadPage();
    }

    protected void loadPage() {
        newPage();
        if ((this.m_selectedPage == 0) || (this.m_selectedPage > this.m_pageCount)) {
            return;
        }
        loadData();
    }

    public synchronized void displayTargetPage() {
        this.m_selectedPage = this.m_targetPage;
        loadPage();
    }

    public abstract void sort(String pm_column, boolean pm_ascending);

    public abstract void loadData();
    
    public FacesContext getFacesCtx(){
        return facesContext;
    }
}
